home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / Quill / Source / ViewTrackers.cpp < prev    next >
Text File  |  1997-05-03  |  7KB  |  261 lines

  1. /*
  2.  *  File:       ViewTrackers.cpp
  3.  *  Summary:       Tracker classes used by CViewContainer.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <3>     5/02/97    JDJ        OnTrackStop now only has a stopPt argument.
  12.  *         <2>     4/06/97    JDJ        Updated for new style TSubPaneIterator.
  13.  *         <1>    10/13/96    JDJ        Created
  14.  */
  15.  
  16. #include "ViewTrackers.h"
  17.  
  18. #include <ZAttribute.h>
  19. #include <ZQDShapes.h>
  20. #include <ZView.h>
  21. #include <ZWindow.h>
  22.  
  23. #include "ViewCommands.h"
  24. #include "ViewContainer.h"
  25.  
  26.  
  27. // ===================================================================================
  28. //    class TSelectPanesTracker
  29. // ===================================================================================
  30.  
  31. //---------------------------------------------------------------
  32. //
  33. // TSelectPanesTracker::~TSelectPanesTracker    
  34. //
  35. //---------------------------------------------------------------
  36. TSelectPanesTracker::~TSelectPanesTracker()
  37. {
  38. }
  39.  
  40.  
  41. //---------------------------------------------------------------
  42. //
  43. // TSelectPanesTracker::TSelectPanesTracker
  44. //
  45. //---------------------------------------------------------------
  46. TSelectPanesTracker::TSelectPanesTracker(CViewContainer* container, const TPoint& startPt, bool extend) : TRubberBandTracker(container, startPt)
  47. {
  48.     mContainer = container;
  49.     
  50.     mExtend = extend;
  51. }
  52.  
  53.  
  54. //---------------------------------------------------------------
  55. //
  56. // TSelectPanesTracker::OnTrackStart
  57. //
  58. //---------------------------------------------------------------
  59. void TSelectPanesTracker::OnTrackStart(const TPoint& startPt)
  60. {
  61.     Inherited::OnTrackStart(startPt);
  62.     
  63.     if (mExtend) {
  64.         TSubPaneIterator iter = mContainer->begin(kRecursive); 
  65.         while (iter != mContainer->end()) {
  66.             TPane* pane = *iter;
  67.             ++iter;
  68.             
  69.             if (mContainer->IsSelected(pane))
  70.                 pane->AddAttribute("Was Selected", new TAttribute);
  71.         }
  72.     
  73.     } else 
  74.         mContainer->Select(nil);
  75. }
  76.  
  77.  
  78. //---------------------------------------------------------------
  79. //
  80. // TSelectPanesTracker::OnTrackContinue
  81. //
  82. //---------------------------------------------------------------
  83. TTracker* TSelectPanesTracker::OnTrackContinue(const TPoint& anchorPt, const TPoint& prevPt, const TPoint& nextPt, bool mouseMoved)
  84. {
  85.     TTracker* tracker = Inherited::OnTrackContinue(anchorPt, prevPt, nextPt, mouseMoved);
  86.  
  87.     if (mSelection.GetWidth() >= mMinSize.width && mSelection.GetHeight() >= mMinSize.height) {
  88.         TRect box = mContainer->LocalToPort(mSelection);
  89.         
  90.         TSubPaneIterator iter = mContainer->begin(kRecursive); 
  91.         while (iter != mContainer->end()) {
  92.             TPane* pane = *iter;
  93.             ++iter;
  94.             
  95.             TRect bounds = pane->GetExtent();
  96.             bounds = pane->LocalToPort(bounds);
  97.             
  98.             if (bounds.Intersects(box) && !bounds.Contains(box)) {
  99.                 if (!mContainer->IsSelected(pane))
  100.                     mContainer->Select(pane, true);
  101.             
  102.             } else {
  103.                 if (mContainer->IsSelected(pane) && !pane->HasAttribute("Was Selected"))
  104.                     mContainer->DoDeselect(pane);
  105.             }
  106.         }
  107.     }
  108.     
  109.     return tracker;
  110. }
  111.  
  112.  
  113. //---------------------------------------------------------------
  114. //
  115. // TSelectPanesTracker::OnTrackStop
  116. //
  117. //---------------------------------------------------------------
  118. void TSelectPanesTracker::OnTrackStop(const TPoint& stopPt)
  119. {
  120.     if (mExtend) {
  121.         TSubPaneIterator iter = mContainer->begin(kRecursive);
  122.         while (iter != mContainer->end()) {
  123.             TPane* pane = *iter;
  124.             ++iter;
  125.             
  126.             if (pane->HasAttribute("Was Selected"))
  127.                 pane->RemoveAttribute("Was Selected");
  128.         }
  129.     }
  130.     
  131.     Inherited::OnTrackStop(stopPt);
  132. }
  133.  
  134. #pragma mark -
  135.  
  136. // ===================================================================================
  137. //    class CResizeTracker
  138. // ===================================================================================
  139.  
  140. //---------------------------------------------------------------
  141. //
  142. // CResizeTracker::~CResizeTracker
  143. //
  144. //---------------------------------------------------------------
  145. CResizeTracker::~CResizeTracker()
  146. {
  147. }
  148.  
  149.  
  150. //---------------------------------------------------------------
  151. //
  152. // CResizeTracker::CResizeTracker
  153. //
  154. //---------------------------------------------------------------
  155. CResizeTracker::CResizeTracker(TPane* pane, const TPoint& anchor, const TPoint& startPt) : TTracker(pane->GetSuperView(), pane->LocalToSuper(startPt))
  156. {
  157.     ASSERT(pane != nil);
  158.     
  159.     mSubPane = pane;
  160.     
  161.     mContainer = dynamic_cast<CViewContainer*>(mSubPane->GetTopView()->FindSubPane("CViewContainer"));
  162.  
  163.     mAnchor = mSubPane->LocalToSuper(anchor);
  164. }
  165.  
  166.  
  167. //---------------------------------------------------------------
  168. //
  169. // CResizeTracker::OnTrackStart
  170. //
  171. //---------------------------------------------------------------
  172. void CResizeTracker::OnTrackStart(const TPoint& startPt)
  173. {
  174.     #pragma unused(startPt)
  175.     
  176.     mOldFrame = mSubPane->GetFrame();
  177. }
  178.  
  179.  
  180. //---------------------------------------------------------------
  181. //
  182. // CResizeTracker::OnTrackConstrain
  183. //
  184. //---------------------------------------------------------------
  185. void CResizeTracker::OnTrackConstrain(const TPoint& anchorPt, const TPoint& prevPt, TPoint& nextPt)
  186. {    
  187.     #pragma unused(anchorPt, prevPt)
  188.     
  189.     if (mContainer != nil && mContainer->SnapToGrid()) {
  190.         TSize gridSize = mContainer->GetGrid();
  191.         
  192.         TPoint constrain = nextPt;
  193.         constrain = mPane->LocalToPort(constrain);
  194.         constrain = mContainer->PortToLocal(constrain);
  195.         
  196.         TPoint constrained;
  197.         constrained.h = ((constrain.h + gridSize.width/2)/gridSize.width)*gridSize.width;
  198.         constrained.v = ((constrain.v + gridSize.height/2)/gridSize.height)*gridSize.height;
  199.         
  200.         nextPt = constrained;
  201.         nextPt = mContainer->LocalToPort(nextPt);
  202.         nextPt = mPane->PortToLocal(nextPt);
  203.     }
  204. }
  205.  
  206.  
  207. //---------------------------------------------------------------
  208. //
  209. // CResizeTracker::OnTrackContinue
  210. //
  211. //---------------------------------------------------------------
  212. TTracker* CResizeTracker::OnTrackContinue(const TPoint& anchorPt, const TPoint& prevPt, const TPoint& nextPt, bool mouseMoved)
  213. {
  214.     #pragma unused(anchorPt, prevPt, mouseMoved)
  215.     
  216.     TRect frame;
  217.     
  218.     if (nextPt.h >= mAnchor.h) {
  219.         frame.left  = mAnchor.h;
  220.         frame.right = nextPt.h;
  221.  
  222.     } else {
  223.         frame.left  = nextPt.h;
  224.         frame.right = mAnchor.h;
  225.     }
  226.     
  227.     if (nextPt.v >= mAnchor.v) {
  228.         frame.top    = mAnchor.v;
  229.         frame.bottom = nextPt.v;
  230.  
  231.     } else {
  232.         frame.top    = nextPt.v;
  233.         frame.bottom = mAnchor.v;
  234.     }
  235.         
  236.     if (frame != mSubPane->GetFrame()) {
  237.         mSubPane->SetFrame(frame);
  238.         
  239.         mSubPane->HandleUpdate();
  240.     }
  241.     
  242.     return this;
  243. }
  244.  
  245.  
  246. //---------------------------------------------------------------
  247. //
  248. // CResizeTracker::OnTrackStop
  249. //
  250. //---------------------------------------------------------------
  251. void CResizeTracker::OnTrackStop(const TPoint& stopPt)
  252. {
  253.     #pragma unused(stopPt)
  254.     
  255.     TRect newFrame = mSubPane->GetFrame();
  256.     if (newFrame != mOldFrame) {
  257.         TCommand* command = new CResizePaneCommand(mSubPane, mOldFrame, newFrame);
  258.         command->Post();
  259.     }
  260. }
  261.